<template>
  <div class="app-container">
      <el-form ref="messageForm" :rules="rules" :model="messageForm" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="标题" prop="title">
          <el-input v-model="messageForm.title" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="messageForm.content" :autosize="{ minRows: 8, maxRows: 20}" type="textarea" placeholder="内容" />
        </el-form-item>
        <el-form-item label="发送时间">
          <el-input v-model="messageForm.send_time" />
        </el-form-item>
        <el-form-item label="接收人">
          <el-input v-model="messageForm.receive" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="messageForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveMessage()">
        确认
      </el-button>
      <el-button v-if="messageForm.status.value === 5" type="danger" @click="handleDelete(messageForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveMessage, getMessageDetail, removeMessageByID } from '@/api/messenger/message'


import waves from '@/directive/waves' // waves directive

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      messageForm: {
        ID: undefined,
        remark: '',
        messageID: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    let messageID = this.$route.query && this.$route.query.messageID
    if (messageID !== undefined) {
      messageID = parseInt(messageID)
      if (!isNaN(messageID)) {
        this.listLoading = true
        getMessageDetail(messageID).then(response => {
          if (response.status === 200) {
            this.messageForm = response.data
            console.log(this.messageForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'message' })
    },
    saveMessage() {
      let _this = this
      this.$refs['messageForm'].validate((valid) => {
        if (valid) {
          _this.messageForm.status = _this.messageForm.status.value;
          if(_this.messageForm.hasOwnProperty('ID')){
            if(_this.messageForm.ID !== undefined){
              _this.messageForm.messageID = _this.messageForm.ID;
            }

          }
          saveMessage(_this.messageForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'message' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeMessageByID(row.ID).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'message' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
